package cn.com.myspring.demo.controller;


import cn.com.myspring.demo.service.IDemoService;
import cn.com.myspring.myformework.annotation.MYAutowired;
import cn.com.myspring.myformework.annotation.MYController;
import cn.com.myspring.myformework.annotation.MYRequestMapping;
import cn.com.myspring.myformework.annotation.MYRequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@MYController
@MYRequestMapping("/demo")
public class DemoController {

    @MYAutowired
    private IDemoService demoService;

    @MYRequestMapping("/query")
    public void query(HttpServletRequest req, HttpServletResponse resp,
                      @MYRequestParam("name") String name) {
        String result = "My name is " + name;
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @MYRequestMapping("/add")
    public void add(HttpServletRequest req, HttpServletResponse resp,
                    @MYRequestParam("a") Integer a, @MYRequestParam("b") Integer b) {
        try {
            resp.getWriter().write(a + "+" + b + "=" + (a + b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @MYRequestMapping("/sub")
    public void add(HttpServletRequest req, HttpServletResponse resp,
                    @MYRequestParam("a") Double a, @MYRequestParam("b") Double b) {
        try {
            resp.getWriter().write(a + "-" + b + "=" + (a - b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @MYRequestMapping("/remove")
    public String remove(@MYRequestParam("id") Integer id) {
        return "" + id;
    }

}
